package com.zhhs.galaxyott.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.zhhs.galaxyott.R;

public class OrderTipforLackofMoneyDialog extends AlertDialog implements android.view.View.OnClickListener {

    private Context mContext;
    private View view;
    private TextView tip_title, tip_msg;
    private EditText creditNum;
    private Button payConfirm_btn, clean_btn, fast_fill50_btn, fast_fill100_btn, payBack_btn;

    private DialogCallback2 callback;
    private String msgString;

    private InputMethodManager imm;

    public OrderTipforLackofMoneyDialog(Context context, String msg, DialogCallback2 callback) {
        super(context);
        this.mContext = context;
        this.callback = callback;
        this.msgString = msg;

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.i("dialog", "this is onCreate-->1");
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawable(new ColorDrawable());
        setContentView(R.layout.order_tip_lackofmoney_dialog);

        imm = ((InputMethodManager) getContext().getSystemService(mContext.INPUT_METHOD_SERVICE));
        // // 默认隐藏EditText
        // getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        initView();
    }

    private void initView() {
        setSize();
        creditNum = (EditText) findViewById(R.id.credit_num);

        payConfirm_btn = (Button) findViewById(R.id.pay_confirm);
        clean_btn = (Button) findViewById(R.id.clean);
        fast_fill50_btn = (Button) findViewById(R.id.fast_fill50);
        fast_fill100_btn = (Button) findViewById(R.id.fast_fill100);
        payBack_btn = (Button) findViewById(R.id.pay_back);
        tip_msg = (TextView) findViewById(R.id.tip_msg);
        tip_msg.setText(StringUtils.toDBC(msgString));

        creditNum.setOnClickListener(this);
        payConfirm_btn.setOnClickListener(this);
        clean_btn.setOnClickListener(this);
        fast_fill50_btn.setOnClickListener(this);
        fast_fill100_btn.setOnClickListener(this);
        payBack_btn.setOnClickListener(this);

        fast_fill50_btn.requestFocus();

    }

    private void setSize() {
        DisplayMetrics dm = new DisplayMetrics();
        // 获取屏幕对象
        getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels; // 屏幕宽（像素，如：480px）
        int height = dm.heightPixels; // 屏幕高（像素，如：800px）
        Window dialogWindow = this.getWindow();
        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        p.height = (int) (height * 0.45);
        p.width = (int) (width * 0.50);
        dialogWindow.setAttributes(p);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
        case R.id.credit_num:

            // creditNum.requestFocus();
            // imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);

            break;
        case R.id.pay_confirm:
            if (creditNum.getText().toString().length() > 0 && Double.parseDouble(creditNum.getText().toString()) >= 0) {
                callback.pay_confirm(Double.parseDouble(creditNum.getText().toString()));
            } else {
                callback.pay_confirm(0.0);
            }

            // dismiss();
            break;
        case R.id.clean:
            // callback.clean();
            creditNum.setText("");

            break;
        case R.id.fast_fill50:
            callback.fast_fill50();

            break;
        case R.id.fast_fill100:
            callback.fast_fill100();

            break;
        case R.id.pay_back:
            // callback.pay_back();
            dismiss();
            break;

        default:
            break;
        }
    }

    public interface DialogCallback2 {

        void pay_confirm(Double price);

        // void clean();

        void fast_fill50();

        void fast_fill100();
        // void pay_back();

    }

}
